package com.example.demojenkins.stream;



import java.util.*;
import java.util.stream.Collectors;

public class StreamDemo {

    /**
     * 转化前 [Costomer{name='szmf1', age=12}, Costomer{name='mary', age=18}, Costomer{name='szmf1', age=21}, Costomer{name='lilei', age=22}, Costomer{name='szmf', age=22}]
     * 转化后 [Costomer{name='lilei', age=22}, Costomer{name='szmf', age=22}, Costomer{name='mary', age=18}, Costomer{name='szmf1', age=33}]
     *
     * @param list
     * @return
     */
    public static List<Costomer> huizhong(List<Costomer> list) {
        return new ArrayList<Costomer>(list.stream().collect(Collectors.toMap(Costomer::getName, c -> c, (costomer52, costomer21) -> {
            int totalAge = costomer52.getAge() + costomer21.getAge();
            costomer21.setAge(totalAge);
            return costomer21;
        })).values());

    }

    /**
     * 转换前 [Costomer{name='ww', age=12}, Costomer{name='mary', age=18}, Costomer{name='szmf1', age=21}, Costomer{name='lilei', age=22}, Costomer{name='szmf', age=22}]
     * 汇总条件 {lilei=cshb, szmf=szmf, mary=cshb, szmf4=szmf, szmf1=szmf, szmf3=szmf, szmf2=szmf} 键值对
     * 转换后 [Costomer{name='ww', age=12}, Costomer{name='cshb', age=40}, Costomer{name='szmf', age=43}]
     *
     * @param list 原数据
     * @param map  汇总条件
     * @return
     */
    public static List<Costomer> huizhong2(List<Costomer> list, Map<String, String> map) {
        return list.stream().map(c -> {
            c.setName(Objects.isNull(map.get(c.getName())) ? c.getName() : map.get(c.getName()));
            return c;
        }).collect(Collectors.toMap(Costomer::getName, c -> c, (costomer52, costomer21) -> {
            int totalAge = costomer52.getAge() + costomer21.getAge();
            costomer52.setAge(totalAge);
            return costomer52;
        })).values().stream().collect(Collectors.toList());
    }


    /**
     * teachers为[Teacher{englishName='szmf', chineseName='数字魔方'}, Teacher{englishName='cshb', chineseName='长沙惠邦'}]
     * students为[Student{teacherName='szmf', name='lilei', age=5}, Student{teacherName='cshb', name='sasa', age=8}]
     * 最终结果为[Teacher{englishName='szmf', chineseName='szmf'}, Teacher{englishName='cshb', chineseName='cshb'}]
     * 获取teacher集合（将Teacher和Student里具有相同值的元素进行替换）
     *
     * @param teachers
     * @param students
     * @return
     */
    public static List<Teacher> huizhong3(List<Teacher> teachers, List<Student> students) {
        return teachers.stream()
                .map(
                        teacher -> {
                            students.stream().filter(student -> Objects.equals(student.getTeacherName(), teacher.getEnglishName())).forEach(s -> {

                                teacher.setChineseName(s.getTeacherName());
                            });
                            return teacher;
                        }
                ).collect(Collectors.toList());
    }

    /**
     * teachers为[Teacher{englishName='szmf', chineseName='数字魔方'}, Teacher{englishName='jd', chineseName='京东'}, Teacher{englishName='cshb', chineseName='长沙惠邦'}]
     * [Student{teacherName='szmf', name='lilei', age=5}, Student{teacherName='cshb', name='sasa', age=8}, Student{teacherName='jd', name='lili', age=3}]
     * [Student{teacherName='数字魔方', name='lilei', age=5}, Student{teacherName='长沙惠邦', name='sasa', age=8}, Student{teacherName='京东', name='lili', age=3}]
     * 获取student集合
     *
     * @param teachers
     * @param students
     * @return
     */
    public static List<Student> huizhong4(List<Teacher> teachers, List<Student> students) {
        return students.stream().map(student -> {
                    teachers.stream().filter(teacher -> Objects.equals(student.getTeacherName(), teacher.getEnglishName())).forEach(t -> {

                        student.setTeacherName(t.getChineseName());
//                                student.setTeacherEnglishName(t.getEnglishName());
//                                转化为[Student{teacherName='数字魔方', teacherEnglishName='szmf', name='lilei', age=5}, Student{teacherName='长沙惠邦', teacherEnglishName='cshb', name='sasa', age=8}, Student{teacherName='京东', teacherEnglishName='jd', name='lili', age=3}]
                    });
                    return student;
                }).collect(Collectors.toList());
    }


}
